<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none" placeholder="请输入搜索内容" v-model="kw">
			</uni-search-bar>
		</view>

		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResults.length!==0">
			<view class="sugg-item" v-for="(item,index) in searchResults" :key="index" @click="gotoDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="20" @click="clearHistory"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<view v-for="(item1,index1) in historys" :key="index1" class="text">
					<text @click="gotoSearch(item1)">{{item1}}</text>
					<uni-icons type="closeempty" color="#6a6a6a" size="14" @click="clearHistory(item1)"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				//搜索的结果列表
				searchResults: [],
				//搜索历史的数据
				historyData: ['aaa', 'sss', 'dddddd']
			};
		},
		computed: {
			historys() {
				//注意 由于数组是引用类型，所以不能直接基于原数组调用reverse方法
				//以免修改元素组中元素的顺序
				//而是应该新建一个内存无关的数组，再进行反转
				return [...this.historyData].reverse()
			}
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 1000)
			},
			onLoad() {
				this.historyData = JSON.parse(uni.getStorageSync('kw') || '[]')
			},
			async getSearchList() {
				//判断搜索词是否为空
				if (this.kw === "") {
					this.searchResults = []
					return
				}
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.kw
				})
				// console.log(res)
				if (res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				this.saveHistory()
			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			//保存搜索记录
			saveHistory() {
				if (this.historyData.indexOf(this.kw) === -1) {
					this.historyData.push(this.kw)
				} else {
					// console.log(this.historyData.indexOf(this.kw))
					this.historyData.splice(this.historyData.indexOf(this.kw), 1)
					this.historyData.push(this.kw)
				}
				uni.setStorageSync('kw', JSON.stringify(this.historyData))
			},
			//清空搜索记录
			clearHistory(e) {
				if (!e) {
					this.historyData = []
				} else {
					this.historyData.splice(this.historyData.indexOf(e), 1)
				}
				uni.setStorageSync('kw', JSON.stringify(this.historyData))

			},
			gotoSearch(item) {
				this.kw = item
				this.input(item)
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	.sugg-list {
		padding: 0 10px;

		.sugg-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 12px;
			padding: 13px 0;
			border-bottom: 1px #efefef solid;

			.goods-name {
				//文字不允许换行（单行文本）
				white-space: nowrap;
				//溢出部分隐藏
				overflow: hidden;
				//文本溢出后，使用...代替
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}

	.history-box {
		.history-title {
			display: flex;
			align-items: center;
			height: 80rpx;
			justify-content: space-between;
			border-bottom: 1px #efefef solid;
			margin: 0 5px;

			text {
				font-size: 14px;
			}
		}

		.history-list {
			display: flex;
			margin: 5px;
			flex-wrap: wrap;

			.text {
				background-color: #f8f8f8;
				padding: 2px 0px 2px 8px;
				border-radius: 2px;
				color: #6a6a6a;
				margin: 5px;

				uni-icons {
					margin-left: 5px;
					margin-right: 5px;
				}
			}
		}
	}
</style>
